/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

Array.prototype.findObj = function(search_key,value)
{
	for(var key in this)
	{
		var obj = this[key];
		
		if(obj[search_key] == value)
		{
			return obj;
		}
	}
	return null;
}
Array.prototype.getIndex = function(value)
{
	for(var i=0;i<this.length;i++)
	{
		if(this[i] == value)
		{
			return i;
		}
	}
	return null;
}
Array.prototype.sortBy = function(property)
{
	this.sort(function(a,b){ return (a[property] - b[property]) }); //verbaasd dat dit kan, propery is toch niet in deze scope??
}
Array.prototype.remove = function(s){
	for(var i=0;i<this.length;i++)
	{ 
		if(s==this[i]) this.splice(i, 1);
	}
}

/**
returns a handler function that when executed calls function of object with the supplied arguments AND the original arguments from the caller (for exampe event obj)
*/
self.bind = function(obj, fun, args) 
{
	var myargs = []
	if (args)
		myargs = args
	
	myargs = Array.prototype.slice.call(myargs)
	
	if (obj === true)
	{
		obj = this;
    }
	var f = typeof fun === "string" ? obj[fun] : fun; //get the function from the obj to call

	return function() {
    	return f.apply(obj, myargs.concat(Array.prototype.slice.call(arguments))); //call the (member) function, append arguments from event trigger
	};
}
self.Element = function(nodeType,props,container,text)
{
	el = document.createElement(nodeType)
	for(var i in props)
	{
		el[i] = props[i]
	}
	if(container)
	{
		container.appendChild(el)
	}
	if(text)
	{
		el.appendChild(document.createTextNode(text))
	}
	return el
}

self.NameToColor = function(name)
{
	r = ((name.charCodeAt(0)%8)+8).toString(16)
	g = ((name.charCodeAt(1)%8)+8).toString(16)
	b = ((name.charCodeAt(2)%8)+8).toString(16)
	
	color = "#" + r + g + b
	return color
}


/*
MyNamespace = {};

MyNamespace.foo = function() {
   this.length = 0;
};
MyNamespace.foo.prototype.getLength = function() {
   return this.length;
};


/*
extend = function(subClass, baseClass) {
   function inheritance() {}
   inheritance.prototype = baseClass.prototype;

   subClass.prototype = new inheritance();
   subClass.prototype.constructor = subClass;
   subClass.baseConstructor = baseClass;
   subClass.superClass = baseClass.prototype;
}

*/

/*Array.prototype.setObj = function(property,obj)
{
	if(this[obj[property]] == null)
	{ 
		this[obj[property]] = obj;
	}
	else
	{
		alert("obj id already there, duplicate id!:"+property)
	}
}*/
/**
add another box for text info under the progressbar
*/
/*VideoPlayer.prototype.addBox = function(name)
{
	el_box = document.createElement("fieldset");
	el_box.className = name;
	el_legend = document.createElement("legend");
	el_legend.appendChild(document.createTextNode(name));
	
	el_content = document.createElement("div");
	el_content.className = "player_box_content";
	el_legend.collapse_el = el_content;
	
	el_box.appendChild(el_legend);
	el_box.appendChild(el_content);

	$(el_legend).click(function(){ $(this.collapse_el).toggle("fast"); });

	this.el_video_player.appendChild(el_box);

	return el_box;
}*/
